<?php
require_once 'MyPdoCounty.class.php';
class TableShopFree extends MyPdoCounty{
	private $tableName = 'shop_free';



	//获取砍价商品列表(全量)
	function _freeGoods_getListFull($pnow,$get_type,$search){
		$limit = $this->config['psize'];	//每页条数
		$start = ($pnow - 1) * $limit;
		if($search){
			if($get_type == 'all'){
				$sql = "select g.banner,g.title,g.price,g.info,f.id as free_id,f.stat as free_stat from shop_goods as g join shop_free as f on g.id = f.goods_id where g.title = ? or g.title like ? limit {$start},{$limit}";
				return $this->dql($sql,array($search,"{$search}%"));
			}else{
				$sql = "select g.banner,g.title,g.price,g.info,f.id as free_id,f.stat as free_stat from shop_goods as g join shop_free as f on g.id = f.goods_id where g.title = ? and f.stat = ? or  g.title like ? and f.stat = ? limit {$start},{$limit}";
				return $this->dql($sql,array($search,$get_type,"{$search}%",$get_type));
			}
		}else{
			if($get_type == 'all'){
				$sql = "select g.banner,g.title,g.price,g.info,f.id as free_id,f.stat as free_stat from shop_goods as g join shop_free as f on g.id = f.goods_id limit {$start},{$limit}";
				return $this->dql($sql,array());
			}else{
				$sql = "select g.banner,g.title,g.price,g.info,f.id as free_id,f.stat as free_stat from shop_goods as g join shop_free as f on g.id = f.goods_id where f.stat = ? limit {$start},{$limit}";
				return $this->dql($sql,array($get_type));
			}
		}
	}




	//更改砍价商品的状态
	function _changeFreeGoodsStat($free_id,$free_stat){
		$sql = "update {$this->tableName} set stat = ? where id = ?";
		return $this->dml($sql,array($free_stat,$free_id));
	}


	//通过商户ID获取砍价商品列表
	function _getFreeListForStoreId($store_id){
		$sql = "select g.banner,g.title,g.price,g.info,f.id as free_id,f.stat as free_stat,f.store_id as store_id from shop_goods as g join shop_free as f on g.id = f.goods_id where f.store_id = ?";
		return $this->dql($sql,array($store_id));
	}


	//用商品id查询砍价商品信息
	function _getFreeInfoForGoodsId($goods_id,$fields = false){
		if(!$fields){
			$fields = '*';
		}
		$sql = "select {$fields} from {$this->tableName} where goods_id = ?";
		return $this->dql($sql,array($goods_id));
	}


	//添加砍价商品
	function _freeGoodsAdd($store_id,$goods_id){
		$sql = "insert ignore into {$this->tableName}(store_id,goods_id) values(?,?)";
		return $this->dml($sql,array($store_id,$goods_id));
	}

}